package com.kolon.business.mOI.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI.controller.MOIJudgeViewController")
public class MOIJudgeViewController implements SimpleController {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/mOI/mOIJudgeView.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// XML URL
		String url = MsgUtil.getMessage("xml.url");
		// 파라미터
		String seq = request.getParameter("seq");
		String sugflag = ComUtil.nullChk(request.getParameter("sugflag")).equals("A00078") ? "pubSubj":"";
		String page = request.getParameter("page");
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String userId = (String)userMap.get("userId");
		String userPw = (String)userMap.get("userPw");
		String cpyCD = (String)userMap.get("CompanyCD");
		// --- [심사 아이디어 제안 보기] --------------------------------------------
		StringBuffer buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=ideaJud&mode=1&PageFlag=e2");
		buffer.append("&Seq=").append(seq);
		buffer.append("&companycd=").append(cpyCD);
		buffer.append("&pType=").append(sugflag);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("BoardDetail");
		Iterator itXml = xmlList.iterator();
		Map map = new HashMap();
		// XML 데이터
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map.put("SugID", el.getChild("SugID").getText());
			map.put("EmpID", el.getChild("EmpID").getText());
			map.put("EmpName", el.getChild("EmpName").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("Title", el.getChild("Title").getText());
			map.put("Content", el.getChild("Content").getText().replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&"));
			map.put("CreateDate", el.getChild("CreateDate").getText());
			map.put("KeepFlag", el.getChild("KeepFlag").getText());
			map.put("SugFlag", el.getChild("SugFlag").getText());
			map.put("ACCEPTDEPTNAME", el.getChild("ACCEPTDEPTNAME").getText());
			map.put("DeptName", el.getChild("DeptName").getText());
			map.put("TelNum", el.getChild("TelNum").getText());
			map.put("StatusCD", el.getChild("StatusCD").getText());
			map.put("JUDGEEMPNAME", el.getChild("JUDGEEMPNAME").getText());
			map.put("SearchAuth", el.getChild("SearchAuth").getText());
			map.put("DeptCD", el.getChild("DeptCD").getText());
		}
		// 심사부서 변경
		element = doc.getRootElement();
		xmlList = element.getChildren("Board2");
		itXml = xmlList.iterator();
		String judgeDept = "";
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			if (el.getChild("Column1") != null)
				judgeDept += el.getChild("Column1").getText() + "_";
		}
		String[] judgeDetpArr = judgeDept.split("_");
		judgeDept = "";
		for (int i=0; i<judgeDetpArr.length; i++) {
			if (i != judgeDetpArr.length-1)
				judgeDept += judgeDetpArr[i] + " > ";
			else
				judgeDept += judgeDetpArr[i];
		}
		map.put("judgeDept", judgeDept);
		// 심사부서 변경 횟수
		element = doc.getRootElement();
		xmlList = element.getChildren("Board3");
		itXml = xmlList.iterator();
		// XML 데이터
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			if (el.getChild("DEPTNUM") != null)
				map.put("DEPTNUM", el.getChild("DEPTNUM").getText());
		}
		// --- [채택 심사내역] ---------------------------------------------------
		buffer.delete(0, buffer.length());
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strQuery=Y&strGubun=JudgeList&JudGubun=idea&companycd=").append(cpyCD);
		// XML DOCUMENT 및 인증 처리
		doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		element = doc.getRootElement();
		xmlList = element.getChildren("Table");
		itXml = xmlList.iterator();
		Map gbMap = null;
		List gbList = new ArrayList();
		// XML 데이터  (나의 제안 수)
		while (itXml.hasNext()) {
			gbMap = new HashMap();
			Element el = (Element)itXml.next();
			gbMap.put("UCODE", el.getChild("UCODE").getText());
			gbMap.put("GBNAME", el.getChild("GBNAME").getText());
			gbMap.put("SUB1", el.getChild("SUB1").getText());
			gbMap.put("SUB2 ", el.getChild("SUB2").getText());
			gbMap.put("CompanyCD", el.getChild("CompanyCD").getText());
			gbMap.put("Flag", el.getChild("Flag").getText());
			gbList.add(gbMap);
		}
		// ---------------------------------------------------------------------
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("map", map);
		modelAndView.addObject("gbList", gbList);
		modelAndView.addObject("page", page);
		return modelAndView;
	}

}

